package com.design.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.design.reggie.common.Result;
import com.design.reggie.dto.DishDto;
import com.design.reggie.dto.SetmealDto;
import com.design.reggie.entity.Category;
import com.design.reggie.entity.Dish;
import com.design.reggie.entity.Setmeal;
import com.design.reggie.entity.SetmealDish;
import com.design.reggie.service.CategoryService;
import com.design.reggie.service.DishService;
import com.design.reggie.service.SetmealDishService;
import com.design.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    /**
     * 分页展示
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDto>> page(int page, int pageSize, String name) {
        //分页构造器
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        // 条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 注意这边要判断name是否为空
        queryWrapper.like(!StringUtils.isEmpty(name), Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        // 分页查询，并返回结果
        setmealService.page(setmealPage, queryWrapper);

        // 对象拷贝,BeanUtils.copyProperties用来拷贝Bean的属性值
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();

        // for循环实现
        // 如果要用List的add方法，那么必须new出List，而不能 List<DishDto> list=null；
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            // 拷贝属性
            BeanUtils.copyProperties(record, setmealDto);
            // 根据id获取分类数据
            Category category = categoryService.getById(record.getCategoryId());
            // 获取分类名
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);

            list.add(setmealDto);
        }

        setmealDtoPage.setRecords(list);

        return Result.success(setmealDtoPage);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result<String> add(@RequestBody SetmealDto setmealDto) {
        setmealService.addWithSetmealDish(setmealDto);

        return Result.success("添加套餐成功");
    }

    @GetMapping("/{setmealId}")
    public Result<SetmealDto> list(@PathVariable long setmealId) {
        SetmealDto setmealDto = setmealService.getByIdWithDish(setmealId);

        return Result.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);

        return Result.success("套餐修改成功");
    }

    /**
     * 启售与停售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> editStatus(@PathVariable int status, String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            Setmeal setmeal = setmealService.getById(s);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }

        return Result.success("修改菜品状态成功");
    }

    /**
     * 根据ids删除套餐，并删除套餐菜品表的相关数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(String ids) {

        setmealService.deleteWithDish(ids);
        return Result.success("删除菜品成功");
    }

    @GetMapping("/list")
    public Result<List<Setmeal>> list(long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, categoryId);
        queryWrapper.eq(status != null, Setmeal::getStatus, status);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);

        return Result.success(list);
    }

    /**
     * 点击套餐图片后展示套餐所包含的菜品
     * @param id 套餐的id
     * @return
     */
    @GetMapping("/dish/{id}")
    public Result<List<SetmealDish>> listDish(@PathVariable String id) {
        // 根据套餐id在setmeal_dish表中查找对应菜品
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);

        return Result.success(list);
    }
}
